#ifndef MAIN_APP_H
#define MAIN_APP_H

#include <gtk/gtk.h>
#include <string>
#include <vector>
#include <map>
#include "../ui/log_grid_dialog.h"
#include "../ui/help_window.h"
#include "../ui/map_window.h"
#include "../ui/ui_manager.h"
#include "../config/config.h"
#include "../config/window_config.h"
#include "../http/http_server.h"
#include "../radio/radio_manager.h"
#include "../database/cty_dat_parser.h"


class MainApp {
public:
    MainApp();
    ~MainApp();
    
    static void initialize(int argc, char *argv[]);
    static int run(int argc, char *argv[]);
    static void cleanup();
    static void cleanup_on_exit();
    static void setup_signal_handlers();
    
    // 窗口创建和销毁回调
    static void on_main_window_show(GtkWidget *widget, gpointer user_data);
    static void on_help_window_destroyed(GtkWidget *widget, gpointer data);
    static void on_log_window_destroyed(GtkWidget *widget, gpointer data);
    static void on_map_window_destroyed(GtkWidget *widget, gpointer data);
    
    // 复选框切换回调
    static void on_log_window_checkbox_toggled(GtkWidget *widget, gpointer data);
    static void on_help_window_checkbox_toggled(GtkWidget *widget, gpointer data);
    static void on_map_window_checkbox_toggled(GtkWidget *widget, gpointer data);
    
    // 波段按钮点击回调
    static void on_dtpl_button_clicked(GtkWidget *widget, gpointer data);
    
    // CW训练按钮点击回调
    static void on_cw_trainer_clicked(GtkWidget *widget, gpointer data);
    
    // 信号连接函数
    static void connect_button_signals(GtkBuilder *builder);
    
    // 配置加载函数
    static std::map<std::string, std::map<std::string, std::string>> loadFunctionKeyConfig();
    static void load_config_to_buttons(GtkWidget *widget, const std::string& qsoType, gpointer user_data);
    static void load_config_to_frequency_wpm(GtkBuilder *builder);
    
    // 配置保存函数
    static void save_frequency_wpm_config(GtkWidget* frequency_entry, GtkWidget* wpm_combo);
    
    // UI工具函数
    static gboolean on_button_right_click(GtkWidget *widget, GdkEventButton *event, gpointer user_data);
    static void clear_input_fields(GtkBuilder *builder);
    static void set_dtpl_red_font(GtkBuilder *builder,std::string dtplname);
    static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
    static gboolean update_qso_memo_callback(gpointer data);
    static void show_function_key_config_dialog(GtkWidget *parent, GtkBuilder *builder);
    
    // 日志窗口访问方法
    static gboolean isLogWindowCreated();
    static void refreshLogWindowData();
    static LogGridDialog* getLogWindowInstance();
    
    // 窗口实例
    static HelpWindow* helpWindowInstance;
    static LogGridDialog* logWindowInstance;
    static MapWindow* mapWindowInstance;
    static GtkWidget* helpWindowWidget;
    static GtkWidget* logWindowWidget;
    static GtkWidget* mapWindowWidget;
    static gboolean helpWindowCreated;
    static gboolean logWindowCreated;
    static gboolean mapWindowCreated;
    static CTYDatParser parser;
    
private:
    
    // HTTP服务器实例
    static HttpServer* httpServer;
    
    // 主窗口关闭标志
    static gboolean isMainWindowClosing;
};

#endif // MAIN_APP_H